package com.jt.assistant.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ToastUtils
import com.jt.assistant.R
import com.jt.assistant.adapter.CarSearchAdapter
import com.jt.assistant.adapter.GenerationAdapter
import com.jt.assistant.base.GlobalConfig
import com.jt.assistant.bean.Car
import com.jt.assistant.bean.Param
import com.jt.assistant.viewmodel.HomeViewModel
import com.tencent.mmkv.MMKV
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_generation.*
import kotlinx.android.synthetic.main.activity_generation.bt_commit
import kotlinx.android.synthetic.main.activity_scan_home.*
import java.text.FieldPosition

/**
 * 生成入库单
 */
class GenerationActivity : BaseActivity<HomeViewModel>() {
    private val mAdapter by lazy {
        GenerationAdapter(
            ArrayList<String>()
        )
    }

    override fun layoutId(): Int = R.layout.activity_generation

    override fun initView(savedInstanceState: Bundle?) {
        set_generation.initCamera(this, GlobalConfig.SCAN_TYPE_ONESHOT)
        with(rv_generation) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    override fun initData() {
    }

    override fun initListener() {
        set_generation.setOnScanListener {

            if (it.isNullOrEmpty()) {
                ToastUtils.showShort("请输入渠道单号")
                set_generation.clearData()
                return@setOnScanListener
            }

            if (mAdapter.data.contains(it)) {
                ToastUtils.showShort("请勿重复添加")
                set_generation.clearData()
                return@setOnScanListener
            }

            mAdapter.data.add(0, it)
            mAdapter.notifyDataSetChanged()
            set_generation.clearData()
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            showMsgDialog(mAdapter.data[position])
        }

        bt_commit.setOnClickListener {
            if (mAdapter.data.isEmpty()) {
                ToastUtils.showShort("请添加渠道单号")
                return@setOnClickListener
            }

            val params: Param = Param::class.java.newInstance()
            params.channelOrderNoList = mAdapter.data
            viewModel.generateReceipt(params)
        }

        viewModel.mGenerateReceipt.observe(this, {
            if (it.isSuccess()) {
                speakString("上传成功")
                finish()
            }
        })
    }

    private fun showMsgDialog(num: String) {
        MaterialDialog(this).show {
            title(null, "是否确认删除:$num?")
            cornerRadius(8f)
            positiveButton(null, "确定") {
                mAdapter.data.remove(num)
                mAdapter.notifyDataSetChanged()
            }
            negativeButton(null, "取消") {
                dismiss()
            }
            lifecycleOwner(this@GenerationActivity)
        }
    }

}